package cattle;

import java.util.Scanner;

public class CattleNum {
    public static Scanner scan = new Scanner(System.in);
    public static boolean flag = false;
    public static boolean count = true;
    public static void main(String[] args) {
        //电脑产生的数字
        int[] computerNumArr=createNum();
        //showComputer(computerNumArr);
        //猜的数组
        for (int i = 5; i >0; i--) {
            int[] myNum=createMyNum();
            showComputer(computerNumArr);
            System.out.println("");
            showMy(myNum);
            System.out.println("");
            //判断
            int result=judge(computerNumArr,myNum);
            if (result==5){
                i=0;
            }else {
                System.out.println("猜错了，你还有"+(i-result)+"次机会");
            }
        }
    }
    //判断
    public static int judge(int[] computerNumArr,int[] myNum){
        int result =0;
        int a = 0;
        int b = 0;
        for (int i = 0; i < computerNumArr.length; i++) {
            for (int j = 0; j < myNum.length; j++) {
                if (computerNumArr[i]==myNum[j]){
                    b++;
                    if (i==j){
                        a++;
                    }
                }
            }
        }

        if (a==4){
            System.out.println("猜对了");
        }else {
            System.out.println("公牛"+a+"只，母牛"+b+"只");
            result++;
        }
        return result;
    }

    //展示自己选的数组
    public static void showMy(int[] myNum){
        System.out.print("自己选的数组：");
        for (int i = 0; i < myNum.length; i++) {
            System.out.print(" "+myNum[i]+" ");
        }
    }

    //猜的数组
    public static int[] createMyNum(){
        int[] numArr = new int[4];
        for (int i = 0; i < numArr.length; i++) {

            try {
                System.out.println("请输入第"+(i+1)+"个数");
                numArr[i]=scan.nextInt();
            }catch (Exception e){
                i--;
                System.out.println("输错了，请输入数字");
                scan.next();
            }
            if (numArr[i]<0 || numArr[i]>9){
                i--;
                System.out.println("输错了，请输入0-9的数字");
            }
            for (int k = 0; k <=i; k++) {
                for (int j = 0; j < k; j++) {
                if (numArr[i]==numArr[j]){
                    i--;
                    System.out.println("重复输入，请输入不重复的数字");
                }
                }
            }
        }

        return numArr;
    }

    //展示电脑产生的数组
    public static void showComputer(int[] computerNumArr){
        System.out.print("电脑产生的数组：");
        for (int i = 0; i < computerNumArr.length; i++) {
            System.out.print(" "+computerNumArr[i]+" ");
        }

    }

    //产生不重复的4个数的数组
    public static int[] createNum(){
        int[] numArr = new int[4];
        for (int i = 0; i <numArr.length ; i++) {
            numArr[i]=randomNum(9,0);
            for (int j = 0; j <i ; j++) {
                if (numArr[i]==numArr[j]){
                    i--;
                    break;
                }
            }
        }
        return numArr;
    }

    //生成数函数
    public static int randomNum(int max,int min){
        return (int)(Math.random()*(max+1-min)+min);
    }
}
